#!/bin/bash
file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "$PWD"
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi

export SVN=`which svn`
##
path_version="/trunk/alps-w1150-20120106                 3    mhf导入2.3android"
path_version="/trunk/ALPS.GB2.MP.V1.10_W_20120214        12   mhf导入2.3android"
path_version="/trunk/ALPS.GB2.MP.V1.10_W_20120214_modem  39   mhf导入2.3modem"
path_version="/trunk/ALPS.ICS.MP.V1_W_20120328           141  mhf导入4.03v1android"
path_version="/trunk/ALPS.ICS.MP.V1_W_20120328_modem     142  pfh导入4.03v1modem"
path_version="/trunk/ALPS.ICS.MP.V2_W_20120504           901  mhf导入4.04v2android"
path_version="/trunk/ALPS.ICS.MP.V2_W_20120504_modem     902  pfh导入4.04v2modem"
##
svn_log_conf=/usr/local/bin/svn-log.conf

function sh_get_svn_replace_path()
{
  local work_copy=$1
  local replace=
  if [ -z "$work_copy" ]; then 
    work_copy=`pwd`
  fi
  local src_url=`svn info $work_copy| sed -n '2p' |awk '{print $2}'`
  local repo_url=`svn info $work_copy|sed -n '3p' |awk '{print $2}'`
  replace=`echo $src_url|sed "s@$repo_url@@g"`
  echo "$replace"
}

function sh_get_svn_repo_import_no()
{
  local work_copy=$1
  local l_replace=
  local l_keyword=path_version
  local result=
  local import_no=
  l_replace=`sh_get_svn_replace_path $work_copy`
  if test -f $svn_log_conf; then
    while read  path  version info; do
      if [ -z "$path" ] || [ -z "$version" ]; then
        continue
      fi
      result=`echo $l_replace |grep $path`
      if [ -n "$result" ]; then
        import_no=$version
        break
      fi
    done < $svn_log_conf
  fi
  echo "$import_no"
}

function parse_svn_log_by_no() 
{
  local f_in=$1
  local l_keyword=$2
  local f_out=$3
  local f_tmp=/tmp/${USER}-svn-log-tmp2.txt
  local l_name=
  local l_replace=`sh_get_svn_replace_path`
  #echo "cat $f_in |grep ^r$l_keyword |awk '{print $3}'"
  l_name=`cat $f_in |grep ^r$l_keyword |awk '{print $3}'`
  l_date=`cat $f_in |grep ^r$l_keyword |awk '{print $5}'|tr -d '-'`
  cat $f_in | sed -n "/r$l_keyword/,/----/p" |grep M -w >$f_tmp
  cat $f_in | sed -n "/r$l_keyword/,/----/p" |grep A -w >>$f_tmp
  cat $f_in | sed -n "/r$l_keyword/,/----/p" |grep D -w >>$f_tmp
  #echo "cat $f_tmp  | sed \"s#^#  $l_date#\" | sed \"s#^#  $l_keyword#\" |sed \"s#^#$l_name#\" | sed "s#$l_replace/##g" | tee $f_out"
  cat $f_tmp  | sed "s#^#  $l_date#" | sed "s#^#  $l_keyword#" |sed "s#^#$l_name#" | sed "s#$l_replace/##g" > $f_out
}

function get_svn_log_v() 
{
  local work_copy=$1
  local version=$3
  local l_versions=
  local f_out=$2
  local f_tmp=/tmp/${USER}-svn-log-tmp2.txt
  local result=
  if [ -n "$version" ]; then
    start_v=`echo "$version" | awk -F':' '{print $1}'`
    end_v=`echo "$version" | awk -F':' '{print $2}'`
  else
    start_v=`sh_get_svn_repo_import_no $work_copy`
    end_v=head
  fi
  #echo "start_v=$start_v end_v=$end_v"
  if [ -n "$start_v" ] && [ -n "$end_v" ]; then
    #echo "${SVN} log $work_copy  -r $start_v:$end_v -v > $f_out"
    ${SVN} log $work_copy  -r $start_v:$end_v -v > $f_out
  else
    if [ -n "$start_v" ]; then
      result=`echo "$start_v" | grep ','`
      if [ -n "$result" ]; then
        l_versions="$result"
      fi
      end_v=head
    else
      start_v=`sh_get_svn_repo_import_no $work_copy`
      if [ -z "$end_v" ]; then
        end_v=head
      fi
    fi  
    rm -rf $f_out
    if [ -n "$l_versions" ]; then
      l_versions="`echo $l_versions | tr ',' ' '`"
      for v in $l_versions
      do
        #echo "${SVN} log $work_copy  -r $v -v > $f_tmp"
        if IsNumeric $v || [ "$v" = "head" ] || [ "$v" = "committed" ]; then
          ${SVN} log $work_copy  -r $v -v > $f_tmp
          cat $f_tmp>>$f_out
        else
           echoTextRed "v=$v svn版本号必须为数字"
        fi
      done     
    else
      ${SVN} log $work_copy  -r $start_v:$end_v -v > $f_out
    fi
  fi
}


function parse_svn_log_by_author()
{
  local f_in=$1
  local l_keyword=$2
  local f_out=$3
  local f_tmp=/tmp/${USER}-svn-log-tmp3.txt
  cat $f_in | sed -n "/$l_keyword/,/----/p" >$f_out
}

function parse_svn_log() 
{
  local f_in=$1
  local f_out=$2
  local l_keyword=$3
  local f_tmp=/tmp/${USER}-svn-log-tmp1.txt
  local l_name=
  local l_version=
  local f_in2=`echo $f_in|sed "s/.txt//g"`-${l_keyword}.txt  
  if [ -z "$l_keyword" ]; then
      l_version=`cat $f_in |grep ^r |awk '{print $1}'|tr -d 'r'` 
  else
    if IsNumeric $l_keyword;then
      l_version=$l_keyword
      l_name=`cat $f_in |grep ^r|grep $l_keyword |awk '{print $3}'`
    else
      l_name=$l_keyword
      l_version=`cat $f_in |grep ^r |grep  $l_keyword |awk '{print $1}'|tr -d 'r'`   
      parse_svn_log_by_author $f_in  $l_keyword $log_f_V
    fi
  fi

  rm $f_tmp -rf; touch $f_tmp
  rm $f_out -rf; touch $f_out
  for v in $l_version
  do
    parse_svn_log_by_no $f_in $v $f_tmp  #单个版本的log
    cat $f_tmp>>$f_out
  done
}


#脚本入口
while test $# != 0
do
    case $1 in
	--username)
	    shift
	    username=$1
	    ;; 
	--password)
	    shift
	    password=$1
	    ;; 

	-r)
	    shift
	    version=$1
	    ;; 
        -t)
	    shift
	    date=$1
	    ;; 
        -u)
	    shift
	    author=$1
	    ;; 

   	-h)
	    shift
	    g_help=$1
	    ;;
   	-p)
	    shift
	    project_name=$1
	    ;;
	--work-copy)
	    shift
	    work_copy=$1
	    ;;
	--by-time)
	    shift
	    by_mode=$1
	    ;; 
  
	-by)
	    shift
	    by_mode=$1
	    ;; 
	
	--url)
	    shift
	    url=$1
	    ;;   
        *)
	   work_copy=$1
	   ;;
       esac
       shift
done
SVNCMD=`which svn`
if [ -z "$work_copy" ]; then
  work_copy=`pwd`
fi

log_f_V_all=/tmp/${USER}-svn-log-v-all.txt
if [ -n "$author" ]; then
log_f_V=/tmp/${USER}-svn-log-v-${author}.txt
log_f_M=/tmp/${USER}-svn-log-m-${author}.txt
log_f_A=/tmp/${USER}-svn-log-a-${author}.txt
log_f_D=/tmp/${USER}-svn-log-d-${author}.txt
log_f_MAD=/tmp/${USER}-svn-log-mad-${author}.txt
log_f_all=/tmp/${USER}-svn-log-files-${author}.txt
else
log_f_V=/tmp/${USER}-svn-log-v.txt
log_f_M=/tmp/${USER}-svn-log-m.txt
log_f_A=/tmp/${USER}-svn-log-a.txt
log_f_D=/tmp/${USER}-svn-log-d.txt
log_f_MAD=/tmp/${USER}-svn-log-mad.txt
log_f_all=/tmp/${USER}-svn-log-files.txt
fi

if [ -n "$version" ]; then
   get_svn_log_v $work_copy $log_f_V_all "$version"
   #${SVN} log $work_copy  -r $version -v > $log_f_V_all
   parse_svn_log $log_f_V_all  $log_f_MAD $author
fi

if [ -n "$date" ]; then
start_date=`echo $date | awk -F':' '{print $1}'`
end_date=`echo $date | awk -F':' '{print $2}'`
else
cat $log_f_MAD
fi

cat $log_f_MAD | awk '{print $5}' | sort | uniq > $log_f_all
cat $log_f_MAD | grep M -w  > $log_f_M
cat $log_f_MAD | grep A -w  > $log_f_A
cat $log_f_MAD | grep D -w  > $log_f_D
echoTextBlue  "分类保存svn修改记录:"
echoTextGreen "$log_f_V_all 原始log信息
$log_f_V $author 原始log信息
-----------------
$log_f_M 修改的文件
$log_f_A 添加的文件
$log_f_D 删除的文件
$log_f_MAD 所有文件
$log_f_all 文件列表"
